package comm

import (
	"gorm.io/gorm"
	"time"
)

// Page 分页参数模型
type Page struct {
	PageNum  int `form:"pageNum,default=1"  json:"pageNum"`
	PageSize int `form:"pageSize,default=10" json:"pageSize"`
}

type TimeReq struct {
	FromTime time.Time `form:"fromTime" time_format:"2006-01-02 15:04:05" time_utc:"1" json:"fromTime"`
	ToTime   time.Time `form:"toTime" time_format:"2006-01-02 15:04:05" time_utc:"1" json:"toTime"`
}

// Paginate 分页
func Paginate(page Page) func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		offset := (page.PageNum - 1) * page.PageSize
		return db.Offset(offset).Limit(page.PageSize).Order("created_at desc")
	}
}

// DescCreateTime 按照修改时间降序
func DescCreateTime() func(db *gorm.DB) *gorm.DB {
	return func(db *gorm.DB) *gorm.DB {
		return db.Order("created_at desc")
	}
}
